<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
//use app\model\Emp;
use app\common\model\mysql\Emp;
class Data extends BaseController
{
	public function index(){
		// 通过门面模式获取
		$res = Db::table("category")->where('id',3)->find();
		echo '<pre>';print_r($res);

		// 通过容器的方式获取实例
		$res = app("db")->table("category")->where('id',3)->find();
		echo '<pre>';print_r($res);

		$res = Db::table("category")->order("id","desc")
			//->limit(2,2)
			->where([
				['id','>',1]
			])
			->select()->toArray();
			echo Db::getLastSql();
		dump($res);
	}

	public function abc(){
		// 输出sql方式一
		$res = Db::table("category")->where('id',2)->fetchSql()->find();
		dump($res);exit;
		// 输出sql方式二
		$res = Db::table("category")->where('id',2)->find();
		echo Db::getLastSql();exit;
		dump($res);
	}

	public function model1(){
		$res = Emp::find(2);
		// print_r($res);
		dump($res->toArray());
	}

	public function model2(){
		$empModel = new Emp();
		$res = $empModel->where('age',30)->limit(5)->order('id','desc')->select();
		// dump($res);
		foreach($res as $val){
			dump($val->name);
		}
	}

	public function demo(){
		/*
		$result = Db::table("demo")->where('id',2)->find();
		$result = Db::table("demo")->order('id','desc')->select()->toArray();
		echo Db::getLastSql();//exit;
		echo '<pre>';print_r($result);
		dump($result);
		*/

		//新增逻辑
		$arr = [
			'title' => '小小鸟',
			'content' => '飞得高高',
			'cat_id' => '2',
			'create_time' => time(),
			'update_time' => time(),
			'status' =>	1,
		];
		/*
		Db::table('demo')->insert($arr);
		echo Db::getLastSql();
		*/


		//更新操作
		/*
		$result = Db::table("demo")->where('id',7)->update(['content'=>'赵传小小鸟好听']);
		echo Db::getLastSql();
		*/

		//删除操作
		$result = Db::table("demo")->where('id',7)->delete();
		echo Db::getLastSql();
	}
}
